import axios from 'axios'

// import { ElMessage } from 'element-plus'
// ElMessage('this is a message.')
// ElMessage({
//     message: 'Congrats, this is a success message.',
//     type: 'success',
// })
// ElMessage({
//     message: 'Warning, this is a warning message.',
//     type: 'warning',
// })
// ElMessage.error('Oops, this is a error message.')


// 组件上下文标识
const baseUrl = process.env.BASE_URL

const http = axios.create({
  timeout: 50000,
  withCredentials: true,
  headers: {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Content-type': 'application/json'
  },
  baseURL: baseUrl
})


// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    switch (error.response.status) {
      case 401:
        // 登录过期错误处理
        break;
      case 500:
        // 服务器错误处理
        break;
      default:
        // ElMessage.error(`${error}`);
    }
  }
  return Promise.reject(error);
};


// request拦截器
http.interceptors.request.use(config => {
  // config.headers["Content-Type"] = "application/json"


  // const token = sessionStorage.getItem('token');
  // if (token) {
  //   config.headers['Authorization'] = `Bearer ${JSON.parse(token)}`;
  // }
  // console.log(config);
  return config
}, 
errorHandler
// error => {
//   Promise.reject(error)
// }
)

// respone拦截器
http.interceptors.response.use(
  response => {
    const res = response.data
    return Promise.resolve(res);
  },
  errorHandler
  // error => {
  //   if (error.response.config.url == '/base/findFacesFromUploadPic.do') {
  //     Notification({
  //       customClass: "notify-error",
  //       title: "错误",
  //       message: error.response.data.msg || "系统错误",
  //     });
  //   }
  //   return Promise.reject(error)
  // }
)

function createAPI(baseURL, http) {
  return function (conf = {}) {
    let _conf = {}
    if (conf.method === 'post') {
      _conf = {
        url: conf.url,
        method: conf.method,
        data: conf.params,
        opts: conf.opts,
        headers: conf.headers,
        responseType: conf.responseType,
      }
    } else {
      _conf = {
        url: conf.url,
        method: conf.method,
        params: conf.params,
        opts: conf.opts,
        responseType: conf.responseType,
      }
    }
    return http(Object.assign(baseUrl, _conf, conf.opts))
  }
}
// 创建接口模板
const API = createAPI(baseUrl, http)
const httpApi = {

  post(url, data = {}, opts = {}) {
    return API({
      method: 'post',
      url: `${url}`,
      params: JSON.stringify(data),
      opts: {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
    })
  },

  get(url, data = {}, opts = {}) {
    return API({
      method: 'get',
      url: `${url}`,
      params: data,
      opts: {
        'header': { 'Content-type': 'application/x-www-form-urlencoded' }
      }
    })
  },
  // post 文件上传请求
  postFile(url, data = {}, opts = {}) {
    return API({
      method: 'post',
      url: `${url}`,
      params: data,
      opts: {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
    })
  },

  // post 文件下载请求
  exportFn(url, data = {}, opts = {}) {
    return API({
      method: 'post',
      url: `${url}`,
      params: data,
      opts: {
        headers: {
          'Content-Type': 'application/json'
        }
      },
      responseType: 'blob'
    })
  }
}

export {
  http,
  httpApi,
  baseUrl
}
